<!--
 * @author  dumeng
-->
<template>
  <div class="question-jianda">
    <el-form
      ref="jiandaform"
      :model="baseform"
      :rules="baserules"
      class="dataForm nomt"
      label-position="right"
      label-width="100px"
    >
      <el-row>
        <el-col :span="24">
          <el-form-item label="题干" prop="stem">
            <custom-textarea
                v-model="baseform.stem"
                :disabled="disabled"
                flag="danxuancontent"
                placeholder="题干这里填写题目描述"
                type="content"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="参考答案" prop="answer">
            <el-input
              v-model.trim="baseform.answer"
              :rows="2"
              :disabled="disabled"
              type="textarea"
              placeholder="请输入参考答案"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="试题解析">
            <el-input
              v-model.trim="baseform.questionAnalysis"
              :rows="2"
              :disabled="disabled"
              type="textarea"
              placeholder="添加试题解析，便于更好的理解答案"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>

import customTextarea from "@/views/skill/question/components/CustomTextarea";

export default {
  name: 'Jianda',
  components:{customTextarea},
  props: {
    type: {
      type: String,
      default: '50'
    },
    getData: {
      type: Object,
      default: () => { }
    },
    disabled: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      baseform: {
        stem: '',
        answer: '',
        questionAnalysis: ''
      },
      baserules: {
        stem: [
          { required: true, message: '请输入题干内容', trigger: 'blur' }
        ],
        answer: [
          { required: true, message: '请输入参考答案', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    if (this.getData && this.getData.id) {
      try {
        if (this.getData.question && this.getData.dealType) { // 错题回显返回的数据
          const answer = JSON.parse(this.getData.question.answer)
          this.$set(this.baseform, 'stem', this.getData.question.stem)
          this.$set(this.baseform, 'questionAnalysis', this.getData.question.questionAnalysis)
          this.$set(this.baseform, 'answer', answer.answers[0])
        } else {
          const answer = JSON.parse(this.getData.answer)
          this.$set(this.baseform, 'stem', this.getData.stem)
          this.$set(this.baseform, 'questionAnalysis', this.getData.questionAnalysis)
          this.$set(this.baseform, 'answer', answer.answers[0])
        }
      } catch (error) {
        console.log(this.getData.question.answer)
        this.$message({
          type: 'error',
          message: '参数异常，请重试!',
          onClose: () => {
            this.$router.back()
          }
        })
      }
    }
  },
  methods: {
    getPostData() {
      let params = ''
      this.$refs.jiandaform.validate(valid => {
        if (valid) {
          const answer = {
            type: this.type,
            answers: [this.baseform.answer]
          }
          params = {
            answer: JSON.stringify(answer),
            stem: this.baseform.stem,
            questionNumber: 1,
            questionAnalysis: this.baseform.questionAnalysis
          }
        }
      })
      return params
    }
  }
}
</script>

<style lang="scss">
.question-jianda {
  .el-form-item.fixed .el-form-item__content {
    display: flex;
    align-items: center;
    .el-input {
      width: auto;
    }
    .score-tip {
      margin-left: 20px;
      color: red;
    }
  }
  .el-radio-group {
    width: 100%;
  }
  .options .el-form-item__content {
    display: flex;
    align-items: center;
    .el-input {
      width: 100%;
      margin-right: 20px;
    }
  }
  .order-fixed {
    margin: 10px 0;
    padding-left: 100px;
  }
  .remove-option {
    cursor: pointer;
  }
  .add-optiopn {
    margin-left: 100px;
  }
  .tip {
    padding-left: 100px;
    padding-bottom: 10px;
    color: red;
    div {
      margin-top: 10px;
    }
  }
}
</style>
